package com.wt.struggle.goodbye.sq.fragment

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.gson.reflect.TypeToken
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.*
import com.wt.struggle.goodbye.info.NetInfo
import com.wt.struggle.goodbye.info.NewsInfo
import com.wt.struggle.goodbye.main.activity.HandPickActivity
import com.wt.struggle.goodbye.main.activity.PartActivity
import com.wt.struggle.goodbye.main.activity.TieDetailsActivity
import com.wt.struggle.goodbye.main.activity.WebViewActivity
import com.wt.struggle.goodbye.sq.activity.PickDetailsActivity
import com.wt.struggle.goodbye.sq.adapter.QuClassifyAdapter
import com.xin.lv.yang.utils.utils.ImageUtil
import kotlinx.android.synthetic.main.classify_fragment_layout.*
import java.util.*

class QuQuanClassifyFragment : ProV4Fragment() {

    override fun handler(what: Int, arg1: Int, netInfo: NetInfo) {
        when (what) {
            Send.GET_BANNER_CODE -> {
                if (netInfo.code == Config.SUCCESS) {
                    val result = netInfo.dataResult
                    if (result != null && result != "" && result != "null") {
                        val arr: ArrayList<NewsInfo> =
                            gson!!.fromJson(result, object : TypeToken<ArrayList<NewsInfo>>() {}.type)

                        if (picBannerViewPager != null) {
                            initPicViewPager(arr)
                        }

                    }
                }
            }

            Send.MY_NEWS_CODE -> {

                if (netInfo.code == Config.SUCCESS) {

                    val result = netInfo.dataResult
                    if (result != null && result != "" && result != "null") {

                        val arr: ArrayList<NewsInfo> =
                            gson!!.fromJson(result, object : TypeToken<ArrayList<NewsInfo>>() {}.type)

                        if (arr.size != 0) {
                            showData(arr)
                        }
                    }
                }
            }

        }

    }

    var resultArr: ArrayList<NewsInfo> = arrayListOf()

    val resImgId = arrayListOf<Int>(
        R.drawable.cycle_pic1,
        R.drawable.cycle_pic2, R.drawable.cycle_pic3, R.drawable.cycle_pic4,
        R.drawable.cycle_pic5, R.drawable.cycle_pic6, R.drawable.cycle_pic7,
        R.drawable.cycle_pic2, R.drawable.cycle_pic3, R.drawable.cycle_pic4,
        R.drawable.cycle_pic5, R.drawable.cycle_pic6, R.drawable.cycle_pic7
    )

    var tempInfo: NewsInfo? = null

    private fun showData(arrList: ArrayList<NewsInfo>) {

        resultArr.clear()

        for (temp in arrList) {

            if (temp.id == 3) {
                this.tempInfo = temp
            } else {
                if (temp.icon != null && temp.icon != "") {
                    resultArr.add(temp)
                }

            }
        }

        resultArr.sortWith(Comparator { p0, p1 ->
            when {
                p0!!.createtime > p1!!.createtime -> 1
                p0.createtime < p1.createtime -> -1
                else -> 0
            }
        })

        resultArr.add(0, tempInfo!!)

        val len = resultArr.size
        when {
            len > 1 -> {

                val info = resultArr[0]

                ImageUtil.getInstance()
                    .loadRoundCircleImage(
                        activity!!,
                        imageViewPic1,
                        Config.GET_IMAGE_URL + info.icon,
                        R.drawable.cycle_pic1,
                        4
                    )

                val adapterList = arrayListOf<NewsInfo>()
                for (i in 1 until resultArr.size) {
                    val temp = resultArr[i]
                    adapterList.add(temp)
                }

                adapter!!.updateDataClear(adapterList)

            }

            len == 1 -> {
                val info = resultArr[0]
                ImageUtil.getInstance()
                    .loadRoundCircleImage(activity!!, imageViewPic1, Config.GET_IMAGE_URL + info.icon, 0, 4)

            }
            else -> {
                picFrameLayout.visibility = View.GONE
            }
        }

    }

    override fun createView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return inflater.inflate(R.layout.classify_fragment_layout, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        send!!.getBanner(handler)

        initStyleAdapter()

        /// 获取精选社区
        send!!.getSQList(1, 0, "", handler)

        picFrameLayout.setOnClickListener {
            val intent = Intent(activity!!, PickDetailsActivity::class.java)
            intent.putExtra("info", resultArr[0])
            intent.putExtra("code", 1)
            startActivity(intent)
        }
    }

    val list = arrayListOf<NewsInfo>()

    var adapter: QuClassifyAdapter? = null

    private fun initStyleAdapter() {
        quRecyclerView.isNestedScrollingEnabled = false
        quRecyclerView.layoutManager = GridLayoutManager(activity, 3)
        adapter = QuClassifyAdapter(activity!!, list)
        quRecyclerView.adapter = adapter

        adapter!!.itemClickListener = object : ItemClickListener {
            override fun onItemClick(position: Int) {
                val intent = Intent(activity!!, PickDetailsActivity::class.java)
                intent.putExtra("info", list[position])
                intent.putExtra("code", 1)
                startActivity(intent)
            }

            override fun onLongClick(position: Int) {
            }

        }

    }


    private val picList = arrayListOf<String>()

    /**
     * 首页轮播图
     */
    private fun initPicViewPager(arr: ArrayList<NewsInfo>) {
        picList.clear()

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            picBannerViewPager.isNestedScrollingEnabled = false
        }

        for (temp in arr) {
            if (temp.pic != null && temp.pic != "" && temp.pic != "null") {
                picList.add(Config.GET_IMAGE_URL + temp.pic!!)
            }
        }

        initViewPager(
            activity!!,
            picBannerViewPager,
            pointRadioGroup,
            picList,
            handler!!,
            2,
            object : OnImageBackListener {

                override fun onImageBack(position: Int) {

                    val info = arr[position]

                    when (info.openType) {

                        1 -> {
                            ///  连接
                            val intent = Intent(activity, WebViewActivity::class.java)
                            intent.putExtra("info", info)
                            startActivity(intent)
                        }

                        2 -> {
                            val intent = Intent(activity, TieDetailsActivity::class.java)
                            intent.putExtra("code", 2)
                            intent.putExtra("data", info)
                            startActivity(intent)
                        }

                        3 -> {
                            val intent = Intent(activity, PartActivity::class.java)
                            intent.putExtra("info", info)
                            startActivity(intent)
                        }

                        else -> {
                            val intent = Intent(activity, TieDetailsActivity::class.java)
                            intent.putExtra("code", 1)
                            intent.putExtra("data", info)
                            startActivity(intent)
                        }

                    }

                }

            })


    }
}